package kr.hkit.androidcomplete01.ch08;

import kr.hkit.androidcomplete01.ch08.HandleEvent.MyView;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;

public class MoveCircle extends Activity {
	private MyView vw;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		vw = new MyView(this);
		vw.setFocusable(true);
		vw.setFocusableInTouchMode(true);
		setContentView(vw);
	}
	
	public class MyView extends View{
		float mX,mY;
		int mColor;
		private Paint paint;
		
		public MyView(Context context){
			super(context);
			mX = 100;
			mY = 100;
			mColor = Color.BLUE;
		}
		
		@Override
		protected void onDraw(Canvas canvas) {
			//super.onDraw(canvas);
			canvas.drawColor(Color.LTGRAY);
			paint = new Paint();
			paint.setColor(mColor);
			paint.setAntiAlias(true);
			canvas.drawCircle(mX, mY, 16, paint);
		}
		
		@Override
		public boolean onKeyDown(int keyCode, KeyEvent event) {
			super.onKeyDown(keyCode, event);
			if(event.getAction()==KeyEvent.ACTION_DOWN){
				switch (keyCode){
				case KeyEvent.KEYCODE_DPAD_LEFT:
					mX-=5;
					invalidate();
					return true;
				case KeyEvent.KEYCODE_DPAD_RIGHT:
					mX+=5;
					invalidate();
					return true;
				case KeyEvent.KEYCODE_DPAD_UP:
					mY-=5;
					invalidate();
					return true;
				case KeyEvent.KEYCODE_DPAD_DOWN:
					mY+=5;
					invalidate();
					return true;
				case KeyEvent.KEYCODE_DPAD_CENTER:
					if(mColor == Color.BLUE){
						mColor = Color.RED;
					}else{
						mColor = Color.BLUE;
					}
					invalidate();
					return true;
				
				}
			}
			return false;
		}
	}
}
